{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-20T02:34:42.855530Z",
     "start_time": "2020-10-20T02:34:42.850961Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-20T02:37:42.565770Z",
     "start_time": "2020-10-20T02:37:42.556184Z"
    }
   },
   "outputs": [],
   "source": [
    "class Net(torch.nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net,self).__init__()\n",
    "        self.conv1=torch.nn.Conv2d(1,10,kernel_size=5)\n",
    "        self.conv2=torch.nn.Conv2d(10,20,kernel_size=5)\n",
    "        self.pooling=torch.nn.MaxPool2d(2)\n",
    "        self.fc=torch.nn.Linear(320,10)\n",
    "    def forward(self,x):\n",
    "        batch_size=x.size(0)\n",
    "        x=F.relu(self.pooling(self.conv1(x)))\n",
    "        x=F.relu(self.pooling(self.conv2(x)))\n",
    "        x=x.view(batch_size,-1)\n",
    "        x=self.fc(x)\n",
    "        return x\n",
    "    \n",
    "model =  Net()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-20T02:50:02.895953Z",
     "start_time": "2020-10-20T02:50:02.653011Z"
    }
   },
   "outputs": [],
   "source": [
    "from torchvision import transforms,datasets\n",
    "from torch.utils.data import DataLoader\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-20T02:51:07.204457Z",
     "start_time": "2020-10-20T02:51:06.896173Z"
    }
   },
   "outputs": [],
   "source": [
    "batch_size=64\n",
    "transform = transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.1307,),(0.3081,))\n",
    "])\n",
    "\n",
    "train_dataset=datasets.MNIST(root='../Day7/dataset/mnist',\n",
    "                            train=True,\n",
    "                            download=True,\n",
    "                            transform=transform)\n",
    "train_loader=DataLoader(train_dataset,\n",
    "                       shuffle=True,\n",
    "                       batch_size=batch_size)\n",
    "test_dataset=datasets.MNIST(root='../Day7/dataset/mnist',\n",
    "                           train=False,\n",
    "                           download=True,\n",
    "                           transform=transform)\n",
    "test_loader=DataLoader(test_dataset,\n",
    "                      shuffle=False,\n",
    "                      batch_size=batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-20T02:51:12.810545Z",
     "start_time": "2020-10-20T02:51:12.806695Z"
    }
   },
   "outputs": [],
   "source": [
    "criterion=torch.nn.CrossEntropyLoss()\n",
    "optimizer=optim.SGD(model.parameters(),lr=0.01,momentum=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-20T02:52:01.544612Z",
     "start_time": "2020-10-20T02:52:01.534221Z"
    }
   },
   "outputs": [],
   "source": [
    "def train(epoch):\n",
    "    running_loss=0.\n",
    "    for batch_index,data in enumerate(train_loader,0):\n",
    "        inputs,target=data\n",
    "        optimizer.zero_grad()\n",
    "        \n",
    "        outputs=model(inputs)\n",
    "        loss=criterion(outputs,target)\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        \n",
    "        running_loss+=loss.item()\n",
    "        if batch_index %300==299:\n",
    "            print('[%d,%5d] loss:%.3f '% (epoch +1, batch_index+1,running_loss/300))\n",
    "            running_loss=0.0\n",
    "            \n",
    "def test():\n",
    "    correct=0\n",
    "    total=0\n",
    "    with torch.no_grad():\n",
    "        for data in test_loader:\n",
    "            images,labels =data\n",
    "            outputs = model(images)\n",
    "            _,predicted=torch.max(outputs.data,dim=1)\n",
    "            total+=labels.size(0)\n",
    "            correct+=(predicted==labels).sum().item()\n",
    "    print('Accuracy on test set: %d %%' % (100*correct/total))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-20T02:57:32.483699Z",
     "start_time": "2020-10-20T02:52:21.360756Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1,  300] loss:0.678 \n",
      "[1,  600] loss:0.198 \n",
      "[1,  900] loss:0.143 \n",
      "Accuracy on test set: 96 %\n",
      "[2,  300] loss:0.114 \n",
      "[2,  600] loss:0.095 \n",
      "[2,  900] loss:0.094 \n",
      "Accuracy on test set: 97 %\n",
      "[3,  300] loss:0.078 \n",
      "[3,  600] loss:0.071 \n",
      "[3,  900] loss:0.072 \n",
      "Accuracy on test set: 98 %\n",
      "[4,  300] loss:0.064 \n",
      "[4,  600] loss:0.062 \n",
      "[4,  900] loss:0.060 \n",
      "Accuracy on test set: 98 %\n",
      "[5,  300] loss:0.057 \n",
      "[5,  600] loss:0.057 \n",
      "[5,  900] loss:0.048 \n",
      "Accuracy on test set: 98 %\n",
      "[6,  300] loss:0.048 \n",
      "[6,  600] loss:0.049 \n",
      "[6,  900] loss:0.048 \n",
      "Accuracy on test set: 98 %\n",
      "[7,  300] loss:0.044 \n",
      "[7,  600] loss:0.044 \n",
      "[7,  900] loss:0.045 \n",
      "Accuracy on test set: 98 %\n",
      "[8,  300] loss:0.041 \n",
      "[8,  600] loss:0.040 \n",
      "[8,  900] loss:0.041 \n",
      "Accuracy on test set: 98 %\n",
      "[9,  300] loss:0.036 \n",
      "[9,  600] loss:0.039 \n",
      "[9,  900] loss:0.037 \n",
      "Accuracy on test set: 98 %\n",
      "[10,  300] loss:0.036 \n",
      "[10,  600] loss:0.032 \n",
      "[10,  900] loss:0.036 \n",
      "Accuracy on test set: 98 %\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    for epoch in range(10):\n",
    "        train(epoch)\n",
    "        test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
